package cn.wd.user.controller;

import cn.wd.common.vo.R;
import cn.wd.model.LoginInfo;
import cn.wd.user.domain.entity.User;
import cn.wd.user.qo.UserQueryObject;
import cn.wd.user.service.IUserService;
import cn.wd.user.utils.TokenManager;
import cn.wd.user.vo.JsonResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
// @Api(value = "用户管理")
public class UserController {

    private final IUserService userService;

    public UserController(IUserService userService) {
        this.userService = userService;
    }

    // @ApiOperation(value = "用户管理", notes = "用户查询分页列表")
    @RequestMapping("/list")
    public R<Page<User>> list(UserQueryObject qo) {
        return R.OK(userService.listUserPage(qo));
    }

    // @ApiOperation(value = "编辑用户信息", notes = "id为null则是新增，否则修改")
    @RequestMapping("/saveOrUpdate")
    public JsonResult<?> saveOrUpdate(@RequestBody User user) {
        if (user.getId() != null) {
            userService.updateById(user);
        } else {
            userService.save(user);
        }
        return JsonResult.success();
    }

    /**
     * 逻辑删除
     *
     * @param id      用户 id
     * @param deleted 操作用户传递过来的状态 是否删除
     * @return 讲 json 数据响应给前端
     */
    @RequestMapping("/delete")
    public R<?> delete(Long id, boolean deleted) {
        if (id == null) {
            return R.FAIL("操作的 id 不能为空！");
        }
        userService.logicDeleteById(id, deleted);
        return R.OK();
    }

    /**
     * 修改用户状态，正常还是禁用
     *
     * @param id     用户id
     * @param status 用户修改的状态
     * @return
     */
    @RequestMapping("/changeStatus")
    public R<?> changeStatus(Long id, Integer status) {
        userService.changeStatus(id, status);
        return R.OK();
    }

    /**
     * 用户重置密码操作
     *
     * @param token       登录用户的 token 信息
     * @param newPassword 用户提交的新密码
     * @param oldPassword 用户的原始密码，旧密码
     * @return 响应修改成功还是修改失败
     */
    @RequestMapping("/restPassword")
    public JsonResult<?> restPassword(String token, String newPassword, String oldPassword) {
        try {
            /*LoginInfo loginInfo = UserController.currentLogin(req.getHeader(TokenManager.TOKEN_NAME));*/

            userService.restPassword(token, newPassword, oldPassword);
            return JsonResult.success();
        } catch (Exception e) {
            return JsonResult.failed(401, e.getMessage());
        }
    }

    // @ApiOperation(value = "登录用户信息", notes = "根据token获取用户信息")
    @RequestMapping("/getLoginInfo")
    public JsonResult<LoginInfo> getLoginInfo(String token) {
        return JsonResult.success(userService.getLoginInfo(token));
    }

    private static LoginInfo currentLogin(@RequestHeader(value = TokenManager.TOKEN_NAME, required = false) String token) {
        return TokenManager.get(token);
    }
}
